import 'package:flutter/material.dart';
import 'package:flutter_lang/lang_index.dart';
import 'package:wallet_home/model/wallet_list_model.dart';
import 'package:wallet_home/tools/theme_utils.dart';
import 'package:wallet_home/widget/coins_manager.dart';

import '../../image_r.dart';
import '../../module_config.dart';
import '../../widget/coins_manager.dart';
import 'custom_app_bar.dart';
import 'search_bar.dart';
import 'search_item_view.dart';

class SearchResultPage extends StatefulWidget {
  @override
  _SearchResultPageState createState() => _SearchResultPageState();
}

class _SearchResultPageState extends State<SearchResultPage> {
  List<CoinsBean> dataSources = [];

  FocusNode _focusNode = FocusNode();

  String searchWord = "";
  int count;

  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(
          context,
          child:SearchBar(
            focusNode: _focusNode,
            enabled: true,
            hideLeft: true,
            hideRight: false,
            hint: "${WXLang.$t("search_property",package: ModuleConfig.moduleName)}",
            leftButtonClick: () {
              _focusNode.unfocus();
              Navigator.pop(context);
            },
            rightButtonClick: () {
              _focusNode.unfocus();
              Navigator.pop(context);
            },
            onChanged: (string) {
              dataSources = <CoinsBean>[];
              searchWord = string;
              count = 0;
              if (string != null && string.length > 0) {
                for (CoinsBean bean in CoinManager().allCoinList) {
                  List<CoinsBean> coins = <CoinsBean>[];
                  if (bean.title.contains(searchWord.toUpperCase())) {
                    coins.add(bean);
                    count++;
                  }
                  if (coins.length > 0) {
                    dataSources.addAll(coins);
                  }
                }
              } else {
                searchWord = "";
              }
              setState(() {});
            },
          ),
        ),
        body: GestureDetector(
            onTap: () {
              _focusNode.unfocus();
            },
            child: _body()));
  }

  Widget _body() {
    if (dataSources.length > 0) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SearchResultCountView(count),
          Expanded(
            child: ListView.separated(
              itemCount: dataSources.length,
              itemBuilder: (_, itemIndex) {
                return ListViewItem(dataSources[itemIndex], () {
                  CoinsBean coin = dataSources[itemIndex];
                  if (coin.isSelected) {
                    CoinManager().saveSelectedCoin(coin.title);
                    // CoinManager().saveSelectedCoin(coin.title);
                  } else {
                    CoinManager().removeSelectedCoin(coin.title);
                    // CoinManager().removeSelectedCoin(coin.title);
                  }
                });
              },
              separatorBuilder: (_, index) {
                return Divider(height: 0.5);
              },
            ),
          )
        ],
      );
    } else {
      if (searchWord.length > 0) {
        return SearchNoResultView();
      } else {
        return Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          color: ThemeUtils.getThemeColor(
              context: context,
              brightnessColor: Color(0xFFEEEEEE),
              darkColor: Colors.black12)
        );
      }
    }
  }
}

class SearchResultCountView extends StatefulWidget {
  final int count;

  SearchResultCountView(this.count);

  @override
  _SearchResultCountViewState createState() => _SearchResultCountViewState();
}

class _SearchResultCountViewState extends State<SearchResultCountView> {
  @override
  Widget build(BuildContext context) {
    return widget.count <= 0
        ? Container()
        : Container(
            height: 20,
            width: double.infinity,
            color: ThemeUtils.getThemeColor(
                context:context,
              brightnessColor: Color(0xffededed),
              darkColor: Colors.black12
            ),
            child: Center(
              child: Text(
                "${WXLang.$t("search_total",package: ModuleConfig.moduleName)}${widget.count}${WXLang.$t("search_result",package: ModuleConfig.moduleName)}",
                style: TextStyle(
                    color:ThemeUtils.getThemeColor(
                        context:context,
                        brightnessColor: Color(0xff333333),
                        darkColor: Colors.white30
                    ),
                    fontSize: 13
                ),
              ),
            ),
          );
  }
}

class SearchNoResultView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(ImageR.imagesNetErrorIcon,
              width: 135, height: 105, package: ModuleConfig.moduleName),
          Container(
            margin: EdgeInsets.only(top: 10),
            width: double.infinity,
            child: Center(
              child: Text(
                "${WXLang.$t("search_sorry",package: ModuleConfig.moduleName)}\n"
                "lili.zhang@btxon.com",
                textAlign: TextAlign.center,
              ),
            ),
          )
        ],
      ),
    );
  }
}
